﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using Excel;

/// <summary>
/// Summary description for ControlBase
/// </summary>
public class ControlBase : UserControl
{
    private static string pageUrlBase;
    private static string urlSuffix;

    public ControlBase()
    {
        try
        {
            urlSuffix = AspConfigurations.Configuration.WebRoot;
            if (AspConfigurations.Common.Right(urlSuffix, 1) != "/")
            {
                urlSuffix = urlSuffix + "/";
            }
            pageUrlBase = urlSuffix;
        }
        catch
        {

        }

    }

    public static string WebRoot
    {
        get
        {
            try
            {
                urlSuffix = AspConfigurations.Configuration.WebRoot;
                if (AspConfigurations.Common.Right(urlSuffix, 1) != "/")
                {
                    urlSuffix = urlSuffix + "/";
                }
                pageUrlBase = urlSuffix;
            }
            catch
            {

            }
            return pageUrlBase;
        }
    }

    /// <summary>
    /// Doc file Excel ra DataTable
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    public System.Data.DataTable ReadFileFromExcel(Stream file)
    {
        try
        {
            //1. Reading from a binary Excel file ('97-2003 format; *.xls)
            IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(file);
            DataSet result = excelReader.AsDataSet();
            if (result.Tables.Count > 0)
            {
                System.Data.DataTable dtOutPut = new System.Data.DataTable();
                dtOutPut = result.Tables[0];
                return dtOutPut;
            }
            else return null;
        }
        catch
        {
            return null;
        }
    }
}